package com.easy.service.impl;

import com.easy.bean.User;
import com.easy.dao.IUserDao;
import com.easy.service.IUserService;
import com.easy.util.DigestUtil;
import com.easy.util.JWTUtil;
import com.easy.util.Page;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.HashMap;
import java.util.List;

@Service
public class UserServiceImpl implements IUserService {

    @Autowired
    IUserDao dao;

    @Override
    public int addUser(User user) {
        user.setPassword(DigestUtil.randomDigest(user.getPassword()));
        return dao.addUser(user);
    }

    @Override
    public int delUser(int id) {
        return dao.delUser(id);
    }

    @Override
    public int editUser(User user) {
        return dao.editUser(user);
    }

    @Override
    public User getUserByID(int id) {
        return dao.getUserByID(id);
    }

    @Override
    public List<User> getUser(User user, Page page) {
        return dao.getUser(user,page);
    }

    @Override
    public int getCount(User user) {
        return dao.getCount(user);
    }


    private boolean eqPassword(String password,String password_data){
        //DigestUtils
        return DigestUtil.decode(password, password_data);
    }

    @Override
    public String doLogin(User user) {
        //1.获取数据库中的数据 byname
        List<User> list=dao.getUserByUsername(user.getUsername());
        if(list.size()==1){
            User user_data=list.get(0);
            //2.比较密码
            boolean bool=eqPassword(user.getPassword(),user_data.getPassword());
            if (bool){
                //获取当前用户信息
                HashMap<String, Object> map = new HashMap();
//                map.put("name", user_data.getName());
                map.put("username", user_data.getUsername());
                map.put("id", user_data.getId());
                //3.生成token
                return JWTUtil.createToken(map);
            }
        }
        return null;
    }

    public static void main(String[] args) {
        //md5加密可以多次加密
        //随机次数加密
        //盐值加密
        String passA="123";
        String digestPass= DigestUtils.md5Hex(passA);
        System.out.println(digestPass);

        String passB="123";
        digestPass= DigestUtils.md5Hex(passB);
        System.out.println(digestPass);

        String passC="123";
        for (int i=0;i<20;i++){
            passC=DigestUtils.md5Hex(passC);
        }
        System.out.println(passC);
    }


    @Override
    public List<User> getUserByUserName(String username) {
        return dao.getUserByUsername(username);
    }

    @Override
    public int saveImg(User user) {
        String originalPath = user.getImg();
        // 使用replaceFirst方法和正则表达式来移除前面的'/img/'
        String processedPath = originalPath.replaceFirst("^/img/", "");
        user.setImg(processedPath);
        return dao.saveImg(user);
    }

    @Override
    public int setPassword(User user) {
        user.setPassword(DigestUtil.randomDigest(user.getPassword()));
        return dao.setPassword(user);
    }


}
